package com.glory.brepda.activity;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Build;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.AuthorityEntity;
import com.glory.brepda.entity.AuthorityListEntity;
import com.glory.brepda.fragment.MainFragment;
import com.glory.brepda.fragment.SearchFragment;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_DownloadFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabEntity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_VersionEntity;
import com.pda.platform.ui.ui_pdaplatform.tablayout.CommonTabLayout;
import com.pda.platform.ui.ui_pdaplatform.tablayout.listener.CustomTabEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_NetUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import butterknife.BindView;

public class MainActivity extends BaseActivity {
    @BindView(R.id.tabLayout)
    CommonTabLayout tabLayout;
    @BindView(R.id.etTitle)
    FreeUI_ClearEditText etTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.content)
    FrameLayout content;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        Constant.AUTHORITY_LIST.clear();
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void initData() {
        initAuthorityList();
        Resources resources = getResources();
        Constant.mEntity.clear();
        mFragments.add(new MainFragment());
        mTabEntities.add(new FreeUI_TabEntity("主界面",
                resources.getDrawable(R.drawable.home_index_select), resources.getDrawable(R.drawable.home_index_normal)));

        if(Constant.CURRENT_USER.equals("admin")){
            mFragments.add(new SearchFragment(Constant.mEntity));
            mTabEntities.add(new FreeUI_TabEntity("查询",
                    resources.getDrawable(R.drawable.home_search_select), resources.getDrawable(R.drawable.home_search_normal)));
        }

        tabLayout.setTabData(mTabEntities, this, R.id.content, mFragments);
        etTitle.requestFocus();
        initEdit(etTitle, s -> {
            etTitle.setText("");
            Intent intent = new Intent();
            for (FreeUI_TabIndexGridEntity item : Constant.mEntity) {
                try {
                    Class<?> aClass = Class.forName(item.getSkipClass());
                    String name = aClass.getSimpleName();

                    if (name.equals(s)) {
                        try {
                            Log.e("SimpleName",name+"----"+s);
                            Class<?> clazz = Class.forName(item.getSkipClass());
                            intent.setClass(this, clazz);
                            intent.putExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY,item.getText());
                            startActivity(intent);
                            break;
                        } catch (Exception var4) {
                            var4.printStackTrace();
                        }
                    }
                } catch (Exception e) {
                    showToast("没有当前扫描到的界面",false);
                    e.printStackTrace();
                }

            }
        });
        detectionUpdate();
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_UP:
                etTitle.setText("");
                etTitle.requestFocus();
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onResume() {
        super.onResume();
        etTitle.requestFocus();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void initAuthorityList() {
        for (AuthorityListEntity.Authority authority : Constant.AUTHORITY_ENTITY.getAUTHORITYLIST()) {
            AuthorityEntity authorityEntity = new AuthorityEntity();
            authorityEntity.setLABELZH(authority.getLABELZH());
            authorityEntity.setNAME(authority.getNAME());
            Constant.AUTHORITY_LIST.add(authorityEntity);
        }
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }

    private void detectionUpdate()    {
        PackageManager manager = getPackageManager();
        PackageInfo info = null;
        try {
            info = manager.getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String version = info.versionName;
        FreeUI_EntityCallBack<FreeUI_VersionEntity> callBack = new FreeUI_EntityCallBack<FreeUI_VersionEntity>
                (new TypeToken<FreeUI_VersionEntity>() {
                }.getType()) {

            @Override
            public void onSuccess(Response<FreeUI_VersionEntity> response) {
                FreeUI_VersionEntity entity = response.body();
                entity.setCurrentVersion(version);
                int code = compareVersion(version,entity.getVersion());
                if(code == -1) {
                    FreeUI_DownloadFragmentDialog downloadFragmentDialog = new FreeUI_DownloadFragmentDialog();
                    downloadFragmentDialog.setData(entity);
                    downloadFragmentDialog.show(getSupportFragmentManager(),"DOWNLOAD");
                }

            }

            @Override
            public void onError
                    (Response<FreeUI_VersionEntity> response) {
                super.onError(response);
            }
        };
        Map<String, String> map = new HashMap<>();
        FreeApi_NetUtils.requestGetNet(Constant.FILE_URL, this, map, callBack);
    }
}
